package main

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"slices"
	"strings"

	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
	"gitee.com/rocket049/go-zbar-scanner"
	"github.com/adrg/sysfont"
)

func getFont() string {
	words := []string{"song", "fang", "kai", "hei"}
	exclude := []string{"simsun.ttf", "simkai.ttf", "simhei.ttf"}
	//words := []string{"kaiti"}
	opts := sysfont.FinderOpts{Extensions: []string{".ttf"}}
	finder := sysfont.NewFinder(&opts)

	for _, f := range finder.List() {
		//println(f.Name)
		for _, w := range words {
			if strings.Index(strings.ToLower(f.Name), w) >= 0 {
				if slices.Contains(exclude, filepath.Base(f.Filename)) {
					continue
				}
				//println(w, f.Name, f.Filename)
				return f.Filename
			}
		}
	}
	return ""
}

func init() {
	fontpath := getFont()
	os.Setenv("FYNE_FONT", fontpath)
}

func main() {
	flag.Parse()
	name := flag.Arg(0)
	qrcode, err := goZbarScanner.ScanFile(name)
	var info string
	if err != nil {
		info = fmt.Sprintf("Error:%v", err.Error())
	} else {
		info = fmt.Sprintf("%v", qrcode)
	}
	showInfo(info)
}

func showInfo(info string) {
	app := app.New()
	w := app.NewWindow("显示二维码信息")
	label := widget.NewLabel(info)
	label.Selectable = true
	w.SetContent(label)
	w.SetMaster()
	w.CenterOnScreen()
	w.ShowAndRun()
}
